// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package androidx.gdata.contacts.xml;

import org.w3c.dom.Node;

import androidx.gdata.contacts.Contact;
import androidx.gdata.contacts.GdEmail;
import androidx.gdata.contacts.GroupMembershipInfo;
import androidx.gdata.xml.ThingWithLinksDeserializer;
import androidx.gdata.xml.CategoryDeserializer;
import androidx.gdata.LinkUrl;
import androidx.gdata.Category;

public class ContactDeserializer extends ThingWithLinksDeserializer<Contact> {
  private CategoryDeserializer categoryBuilder = new CategoryDeserializer();
  private GdEmailDeserializer emailBuilder = new GdEmailDeserializer();
  private GroupMembershipInfoDeserializer gmiBuilder = new GroupMembershipInfoDeserializer();

  public Contact build(Node contactNode) {
    node = contactNode;
    String id = getText("id");
    String updated = getText("updated");
    Node categoryNode = getChildNode("category");
    Category category = categoryBuilder.build(categoryNode);
    
    String title = getText("title");
/*    
    String artist = getText("artist");
    String title = getText("title");
    String mbid = getText("mbid");
    String url = getText("url");
    List<Node> imageNodes = getChildNodes("image");
    ImageUrl[] images = new ImageUrl[imageNodes.size()];
    int i = 0;
    for (Node imageNode : imageNodes) {
      images[i++] =imageBuilder.build(imageNode);
    }
    return new Album(artist, title, mbid, url, images);
*/
    LinkUrl[] links = getLinks();
    Node emailNode = getChildNode("gd:email");
    GdEmail email = null;
    if (emailNode != null) {
      email = emailBuilder.build(emailNode);
    }
    GroupMembershipInfo groupMembership = null;
    Node gmiNode = getChildNode("gContact:groupMembershipInfo");
    if (gmiNode != null) {
      groupMembership = gmiBuilder.build(gmiNode);
    }
    
    return new Contact(id, updated, category, title, links, email, groupMembership);
  }
}